package com.shanghaionstar.discover.geofence;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.GeofenceInfoEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.geofence.iview.IGeofenceDeatilView;
import com.shanghaionstar.discover.geofence.presenter.GeofenceDetailPresenter;
import com.shanghaionstar.enumtype.AlertType;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.TopLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by 7828wc on 16/7/21.
 */
@ContentView(R.layout.discover_geofence_detail)
public class GeofenceDetailActivity extends SwipeActivity implements IGeofenceDeatilView {
    @FindViewById
    private TextView geofence_name;
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private ToggleButton toggle_geofence_name;
    @FindViewById
    private CustomLineLayout center_poi, geofence_mobile, geofence_range, geofence_rule, geofence_effective_day;
    @FindViewById
    private CommonProgressButton btn_delete, btn_update;
    @FindViewById
    private ProgressLayout loading_progress;

    private GeofenceDetailPresenter presenter;

    @Override
    public BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new GeofenceDetailPresenter(getScheduler());
        presenter.setView(this);
        presenter.getIntent();
        initView();
        setListener();
    }


    private void initView() {
        btn_delete.setButtonBg(R.drawable.stroke_wihte_bg);
        btn_delete.setTextColor(R.color.color_1762cb);
        top_layout.setRightBtnRes(R.drawable.geo_loc);
        top_layout.setOnRightClickListener("", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              presenter.launchPreview();
            }
        });

        toggle_geofence_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                presenter.onCheck(!toggle_geofence_name.isSelected());
                toggle_geofence_name.setSelected(!toggle_geofence_name.isSelected());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getIntent();
        presenter.initData();
    }


    private void setListener() {
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //执行删除
                AlertDialogUtil.showAlertDialog(GeofenceDetailActivity.this, R.string.geofence_submit_dialog_delete, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        showProgressLayout(true);
//                        BusinessUtil.sendReportByClickType(GeofenceDetailActivity.this, FunctionNameFindId.Delete_Button.getStringValue());
                        BusinessUtil.sendReportByClickType(GeofenceDetailActivity.this, FunctionNameFindId.GeoDefenceDeleteButton.getStringValue());
                        presenter.deleteGeofencing();
//                        geofenceUtil.processGeoFencing(detailGeoFenceInfo, Constants.OPERATE_TYPE_DELETE);
                    }
                }, null);
            }
        });
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(GeofenceDetailActivity.this, FunctionNameFindId.GeoDefenceUpdateButton.getStringValue());
                presenter.launchUpdate();
            }
        });
    }

    @Override
    public void setLoading(boolean isLoading) {
//        btn_delete.showLoading(isLoading);
        loading_progress.setVisibility(isLoading?View.VISIBLE:View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        presenter.onFailed();
    }

    @Override
    public void onReponseSuccess(Object object) {
        if(CommonUtil.isNull(object)){
            presenter.launchHome();
        }else {
            presenter.initData();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        this.setIntent(intent);
        presenter.getIntent();
    }


    @Override
    public Activity onActivity() {
        return this;
    }

    @Override
    public void initData(GeofenceInfoEntity detailGeoFenceInfo) {
        geofence_name.setText(detailGeoFenceInfo.getGeoFencingName());
        center_poi.setRightText(detailGeoFenceInfo.getCenterPoiName());
        geofence_mobile.setRightText(detailGeoFenceInfo.getMobilePhone());
        String range = detailGeoFenceInfo.getRange();
        if (!CommonUtil.isNull(range)
                && (range.toUpperCase(Locale.CHINA).contains(getString(R.string.geofence_range_unit_km)) || range
                .toUpperCase(Locale.CHINA).contains(getString(R.string.geofence_range_unit_m)))) {
            geofence_range.setRightText(range);
        } else {
            geofence_range.setRightText(range + presenter.getRangeUnit(detailGeoFenceInfo));
        }
        presenter.setRule(detailGeoFenceInfo);
        geofence_rule.setRightText(detailGeoFenceInfo.getRule());
        try {
            String date = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("yyyy-MM-dd")
                    .parse(detailGeoFenceInfo.getExpiredDate()));
            geofence_effective_day.setRightText(date);
        } catch (ParseException e) {
            e.printStackTrace();
            geofence_effective_day.setRightText(detailGeoFenceInfo.getExpiredDate());
        }

        if (!CommonUtil.isNull(detailGeoFenceInfo.getGeoFencingStatus())
                && detailGeoFenceInfo.getGeoFencingStatus().equalsIgnoreCase(AlertType.ON.name())) {
            toggle_geofence_name.setSelected(true);
        } else {
            toggle_geofence_name.setSelected(false);
        }
    }

    @Override
    public void onFailed(boolean isFailed) {
        toggle_geofence_name.setSelected(!toggle_geofence_name.isSelected());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.finish();
    }

    //    @Override
//    public void onClick(View v) {
//
//    }
}
